package com.jacken.mars.commons.mq.base.consumer;

import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;

import java.util.List;

/**
 * @author wangqiang
 * @version 1.0
 * @date 2021/1/3 16:32
 */
public class Consumer {

    public static void main(String[] args) throws Exception {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("group-01");
        consumer.setNamesrvAddr("114.55.146.111:9876");
        consumer.subscribe("Async-Producer-Topic", "Tag-02");
        //可以设置为广播模式  默认为负载均衡模式
        consumer.setMessageModel(MessageModel.BROADCASTING);
        //设置回调函数
        //接收消息内容
        consumer.registerMessageListener((MessageListenerConcurrently) (list, consumeConcurrentlyContext) -> {
            System.out.println("接收消息:" + JSONObject.toJSONString(list));
            for (MessageExt messageExt : list) {
                System.out.println(messageExt.getBody().toString());
            }
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        });
        consumer.start();
    }
}
